#ifndef bnb_controller_h
#define bnb_controller_h

#include "singleton.h"
#include "logger.h"
#include "bnb_video_context.h"

extern "C" {
#include "lua.h"
}

/**
  * Termination handler for the Logger.
  */
void bnb_terminate (int exit_status);

class BNBController : public Singleton<BNBController> {
  friend class Singleton<BNBController>;

  public:
    /**
      * Initializes the SDL library, then each component sequentially.
      */
    void initialize ();

    /**
      * Calls the logger quit function, which in turn will call the termination
      * function.
      */
    inline void terminate ();

    /**
      * Get the Lua state.
      */
    inline lua_State* getLuaState ();

  private:
    inline BNBController ();
    inline ~BNBController ();

    /**
      * Initializes the SDL library.
      */
    void initSDL ();
    /**
      * Initializes Lua.
      */
    void initLua ();

  private:
    BNBVideoContext* vidContext;
    lua_State* luaState;
};

BNBController::
BNBController () 
  : vidContext(NULL), luaState(NULL) {
  // Nothing to do.
}

/**
  * Kill each component sequentially.
  */
BNBController::
~BNBController () {
  SDL_Quit();
  BNBVideoContext::kill(); 
}

void BNBController::
terminate () {
  LOG ("Terminating BnB Controller and closing log system.");
  logger_quit();
}

lua_State* BNBController::
getLuaState () {
  return luaState;
}

#endif

